<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="单据编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="单据编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" prop="vendorId" width="120" :formatter="vendorName" >

      </el-table-column>
      <el-table-column label="单据编号" prop="orderNo" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="商品信息" prop="goodsId" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="单据日期" prop="createDate" width="100" />
      <el-table-column label="操作员" prop="userId" width="100" />
      <el-table-column label="金额合计" prop="price" width="100" />
      <el-table-column label="支付定金" prop="deposit" width="100" />
      <el-table-column label="状态" align="center" width="100" :formatter="stateName">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="shenghe(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配置对话框 -->
    <el-dialog title="添加采购订单" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form"  label-width="100px" :inline="true" style="width: 100%;padding: 20px">
        <el-form-item label="供应商" prop="属性">
          <el-select v-model="form.vendorId" placeholder="请选择供应商" size="mini">
            <el-option
              v-for="item in vendors"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>
        &emsp;&emsp;&emsp;
        <el-form-item>
          <el-button icon="el-icon-search" @click="showGoodsListDialog" size="mini">选择商品</el-button>
        </el-form-item>

        <div style="border-top: 1px solid #e0e0e0;margin-top:20px;margin-bottom:20px"></div>

        <el-table   :data="selectGoodsList" >
          <el-table-column label="名字" prop="name"  />
          <el-table-column label="规格" prop="specification"  />
          <el-table-column label="型号" prop="model"  />
          <el-table-column label="颜色" prop="color" />
          <el-table-column label="单位" prop="unit"  />
          <el-table-column label="价格" prop="costPrice"  />
          <el-table-column label="数量" prop="number" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.number"  @input="getSum" placeholder="" size="mini"  style="width: 100px" type="number"/>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <div style="width: 100%">
          <el-input  type="textarea" v-model="form.属性" placeholder="订单备注" />
        </div>
        <br/>
        <el-form-item label="结算账户" prop="属性">
          <el-select v-model="form.accountId" placeholder="" size="mini" >
            <el-option
              v-for="item in accountList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单总额" prop="属性">
          <el-input v-model="form.totalAmount"  placeholder="" size="mini" :readonly="'readonly'"/>
        </el-form-item>
        <el-form-item label="优惠金额" prop="属性">
          <el-input v-model="form.discountAmount" @input="haschange" value="0" placeholder="" size="mini"/>
        </el-form-item>
        <el-form-item label="优惠后金额" prop="属性">
          <el-input v-model="form.realAmount"  placeholder="" size="mini" :readonly="'readonly'"/>
        </el-form-item>
        <el-form-item label="支付定金" prop="属性">
          <el-input v-model="form.depositAmount" placeholder="" size="mini"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定添加</el-button>
        <el-button type="primary" @click="submitForm1">添加并审核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择商品的弹窗 -->
    <el-dialog title="选择要采购的商品" :visible.sync="goodsListOpen" width="1000px" append-to-body>

      <el-table  ref="goodsTable"  :data="goodsList" @selection-change="handleGoodsSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="名字" prop="name"  />
        <el-table-column label="规格" prop="specification"  />
        <el-table-column label="型号" prop="model"  />
        <el-table-column label="颜色" prop="color" />
        <el-table-column label="单位" prop="unit"  />
        <el-table-column label="价格" prop="costPrice"  />
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="goodsListOpen = false">确定选择</el-button>
        <el-button @click="goodsListDialogCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {getVendors, getGoods, getAccounts, addOrder, listOrder, updateOrder, shengHe} from "@/api/purchase/order";

export default {
  name: "cgorder",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      //供应商
      vendors: [],
      accountList: [],

      showSearch: true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      //商品列表
      goodsList: [],
      //选中的商品
      selectGoodsList: [],
      //第一个弹窗
      open: false,
      //第二个弹窗
      goodsListOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: ""
      },
      // 表单参数
      form: {
        vendorId: "", //供应商
        orderNo: "", //订单号
        accountId: "", //结算账户ID
        depositAmount: "", //定金
        discountAmount: "",//优惠金额
        totalAmount: "",//订单总额
        realAmount: "",//真实需要支付金额
      },
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  created() {
    this.getList();
    this.getVendors();
  },
  methods: {
    cancel() {
      this.open = false;
    },
    shenghe(val){
      shengHe(val).then(res=>{
        this.$modal.msgSuccess("操作成功");
        this.getList()
      })
    },
    vendorName(val){
      var a;
      this.vendors.forEach(item=>{

        if (item.id === val.vendorId) {
          a= item.name;
        }
      })
      return a;
    },
    stateName(val){
      if (val.state === 0) {
        return '待审核';
      }else if (val.state === 1) {
        return '已审核';
      } else if (val.state === 2) {
        return '已完成';
      } else {
        return '已退款';
      }
    },
    goodsListDialogCancel(){
      this.goodsListOpen = false;
      this.selectGoodsList = [];
      this.$refs.goodsTable.clearSelection();
    },
    showGoodsListDialog(){
      this.goodsListOpen = true;
    },
    getSum(){
      var list = this.selectGoodsList;
      var total = 0;
      list.forEach(item=>{
        if (typeof (item.number) == "undefined") {
          total += 0;
        } else {
          total+= item.costPrice*item.number;
        }
      })
      this.$set(this.form, 'totalAmount',total )
    },
    haschange(val){
      this.$set(this.form, 'realAmount',this.form.totalAmount-val )
    },


    /** 查询列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getVendors(){
      getVendors().then(res=>{
        this.vendors=res.rows;
      });
    },
    getGoods(){
      getGoods().then(res=>{
        this.goodsList=res.rows;
      });
    },
    getAccounts(){
      getAccounts().then(res=>{
        this.accountList=res.rows;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleGoodsSelectionChange(selection){
      this.selectGoodsList = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.form={};

      this.getGoods();
      this.getAccounts();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form['goodsList'] = this.selectGoodsList;
      this.form['state']=0;
      console.log(this.form);
      //发送请求，保存订单
      addOrder(this.form).then(res=>{
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });

      this.open = false;
    },
    submitForm1: function() {
      this.form['goodsList'] = this.selectGoodsList;
      this.form['state']=1;
      console.log(this.form);
      //发送请求，保存订单
      addOrder(this.form).then(res=>{
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });

      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
